package com.ubs.web.client.panel.login;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.ubs.dom.Account;
import com.ubs.web.client.AbstractAsyncCallback;
import com.ubs.web.client.AlertDialogBox;
import com.ubs.web.client.AppContext;
import com.ubs.web.client.security.SecurityContext;

public class LoginInputsGrid extends Grid {

	private static LoginInputsGrid loginInputsGrid;

	public static LoginInputsGrid getSingleton() {
		if (loginInputsGrid == null)
			loginInputsGrid = new LoginInputsGrid();
		
		return loginInputsGrid;
	}
	
	private LoginInputsGrid() {
		super(3, 3);
		
		final TextBox usernameTextBox = new TextBox();
		final PasswordTextBox passwordTextBox = new PasswordTextBox();

		final Label usernameLabel = new Label(AppContext.getMessages().userNameLabel());
		final Label passwordLabel = new Label(AppContext.getMessages().userPasswordLabel());

		usernameLabel.setStyleName("login01");
		passwordLabel.setStyleName("login01");

		final Button loginButton = new Button();
		loginButton.setText(AppContext.getMessages().loginButton());
		loginButton.addStyleName("sendButton");
		
		final CheckBox rememberMeBox = new CheckBox(AppContext.getMessages().rememberMeLabel());
		rememberMeBox.setStyleName("rememberMeLabel");

		setSize("200px", "73px");
		
		setWidget(0, 0, usernameLabel);
		setWidget(0, 1, usernameTextBox);
		setWidget(1, 0, passwordLabel);
		setWidget(1, 1, passwordTextBox);
		setWidget(1, 2, loginButton);
		setWidget(2, 1, rememberMeBox);

		// Focus the cursor on the name field when the app loads
		usernameTextBox.setFocus(true);
		usernameTextBox.selectAll();
		
		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				loginToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					loginToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void loginToServer() {
				String name = usernameTextBox.getText();
				if (name.isEmpty()) {
					return;
				}

				AppContext.getPortalService().loginAccount(
						usernameTextBox.getText(), passwordTextBox.getText(),
						new AbstractAsyncCallback<Account>() {

							@Override
							public void onSuccess(Account account) {

								if (account != null) {
									SecurityContext.setAccount(account);
								} else {
									AlertDialogBox
											.showInfo("No login and passwrd have been found");
								}
							}

						});
			}
		}

		// Add a handler to send the data to the server
		MyHandler handler = new MyHandler();
		loginButton.addClickHandler(handler);
		usernameTextBox.addKeyUpHandler(handler);
		passwordTextBox.addKeyUpHandler(handler);
		
	}
}
